﻿//封装基本操作方法
(function ($){
    $Mrc = {};
    $Mrc.host = '';
    //基本Ajax脚手架
    $Mrc.BaseAjax = function (type, url, data, callback)
    {
        var layerIndex = layer.load(2);//请求加载框
        $.ajax({
            type: type,
            url: $Mrc.host + url,
            data: data,
            dataType: "json",
            complete: function (xhr) {
                layer.close(layerIndex);
            },
            success: function (result) {
                var isResult = ("Status" in result) && ("Msg" in result);
                if (isResult)
                {
                    if (result.Status === ResultStatus.Failed) {
                        layerAlert(result.Msg || "操作失败");
                        return;
                    }
                    if (result.Status === ResultStatus.NotLogin) {
                        layerAlert(result.Msg || "未登录或登录过期，请重新登录");
                        return;
                    }
                    if (result.Status === ResultStatus.Unauthorized) {
                        layerAlert(result.Msg || "权限不足，禁止访问");
                        return;
                    }

                    if (result.Status=== ResultStatus.OK)
                    {
                        if (typeof callback === 'function') {
                            callback(result);
                        }
                    }
                }
                else
                {
                    callback(result);
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                console.log("请求失败：" + url + "data:" + JSON.stringify(data));
            }
        });
    }


    $Mrc.alert = function (msg, callBack) {
        layerAlert(msg, callBack);
    }
    $Mrc.confirm = function (msg, callBack) {
        layerConfirm(msg, callBack);
    }
    $Mrc.msg = function (msg) {
        layerMsg(msg);
    }

    //Alert弹窗
    function layerAlert(msg, callBack) {
        msg = msg === null ? "" : msg;/* layer.alert 传 null 会报错 */
        var type = 'warning';
        var icon = "";
        if (type === 'success') {
            icon = "fa-check-circle";
        }
        if (type === 'error') {
            icon = "fa-times-circle";
        }
        if (type === 'warning') {
            icon = "fa-exclamation-circle";
        }
         
        var idx;
        idx = layer.alert(msg, {
            icon: icon,
            title: "系统提示",
            btn: ['确认'],
            btnclass: ['btn btn-primary'],
        }, function () {
            layer.close(idx);
            if (callBack)
                callBack();
        });
    }
    //确认弹窗
    function layerConfirm(content, callBack) {
        var idx;
        idx = layer.confirm(content, {
            icon: "fa-exclamation-circle",
            title: "系统提示",
            btn: ['确认', '取消'],
            btnclass: ['btn btn-primary', 'btn btn-danger'],
        }, function () {
            layer.close(idx);
            callBack();
        }, function () {

        });
    }
    //消息弹窗
    function layerMsg(msg, callBack) {
        msg = msg === null ? "" : msg;/* layer.msg 传 null 会报错 */
        layer.msg(msg, { time: 2000, shift: 0 });
    }

    //附加Window对象
    Window.$Mrc = $Mrc;
})($);

